package com.poobo.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import com.poobo.base.impl.BaseDaoImpl;
import com.poobo.dao.ISysAccountDao;
import com.poobo.entity.TbSysAccount;
import com.poobo.util.pager.Page;
import com.poobo.util.str.StrUtil;

@Repository
public class SysAccountDaoImpl extends BaseDaoImpl implements ISysAccountDao {
	@Override
	public Page findTbAccount(TbSysAccount tbSysAccount,Page page) {
		String baseHql="from TbSysAccount where 1=1";
		String countHql = "select count(1) from TbSysAccount where 1=1 ";
		StringBuffer where = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		if(tbSysAccount.getStatus()!=null){
			where.append("and status =?");
			params.add(tbSysAccount.getStatus());
		}
		if(StrUtil.isNotNull(tbSysAccount.getLoginName())){
			where.append("and loginName =?");
			params.add(tbSysAccount.getLoginName());
		}
		if(StrUtil.isNotNull(tbSysAccount.getName())){
			where.append("and name ="+tbSysAccount.getName());
			params.add(tbSysAccount.getName());
		}
		String orderBy = " order by id desc";
		page.setBaseHql(baseHql + where.toString() + orderBy);
		page.setCountHql(countHql + where.toString());
		page.setParam(params);
		return getPageByHql(page);
	}

	@Override
	public List<TbSysAccount> getSysTbAccount(String loginName, String pwd) {
		String hql="from TbSysAccount where 1=1 and loginName=? and loginPwd=?";
		Object[] objects={loginName,pwd};
		return (List<TbSysAccount>) findByParams(hql, objects);
	}

	@Override
	public TbSysAccount finSysAccountById(Integer id) {
		return selectObjectById(TbSysAccount.class, id);
	}

	@Override
	public void updateSysAccount(TbSysAccount sysAccount) {
		saveOrUpdate(sysAccount);
	}
}
